﻿@using Shared.Extensions

@{
    //bool isSelectedComponent =
    //    FormDesigner.IsSelectedComponent(ComponentData);
}

<div class="widget-wrapper">
  @*  @if (isSelectedComponent)
    {
        <ComponentMover Component="@ComponentData"
                        ComponentsInParentRow="ComponentRow" />
    }*@

    <ul class="nav nav-tabs" role="tablist">
        @foreach (var tabContainer in TabContainerList)
        {
            var activeTabClass = IsTabOpened(tabContainer) ? "active" : "";

            <li class="nav-item">
                <a class="nav-link d-flex flex-row clickable @activeTabClass"
                @onclick="@(async () => await OpenTabAsync(tabContainer))"
                @onclick:preventDefault="true"
                @onclick:stopPropagation="true">
                  
                    <span class="tab-title">@(tabContainer.MutLanguage.GetLanguage())</span>
             
                </a>
            </li>
        }
        <li class="nav-item">
            <a class="nav-link d-flex flex-row clickable"
               title="Add Tab"
            @onclick="@(async () => await AddTabAsync())"
            @onclick:preventDefault="true">
             
            </a>
        </li>
    </ul>

    @foreach (var tabContainer in TabContainerList)
    {
        @if (IsTabOpened(tabContainer) || IsThereOnlyOneTab())
        {
            <ContainerRuning ContainerData="tabContainer" />
        }
    }
</div>

@code {
    [CascadingParameter(Name = "Root")]
    public FormRuning FormDesigner { get; set; }

    [Parameter]
    public ComponentDto ComponentData { get; set; }

    [Parameter]
    public List<ComponentDto> ComponentRow { get; set; }

    [Parameter]
    public List<ContainerDto> TabContainerList { get; set; }

    private ContainerDto ActiveTabContainer = null;

    private async Task OpenTabAsync(ContainerDto tabContainer)
    {
        ActiveTabContainer = tabContainer;
        await FormDesigner.SelectContainerAsync(ActiveTabContainer);
    }

    private async Task AddTabAsync()
    {
        var newTabContainer = new ContainerDto(ContainerType.Tab)
            {
                MutLanguage = new MutLanguage() { zh_CN = $"Tab {TabContainerList?.Count + 1}" }

            };

        TabContainerList.Add(newTabContainer);
        await OpenTabAsync(newTabContainer);
    }

    private bool IsTabOpened(ContainerDto tabContainer)
    {
        return ActiveTabContainer == tabContainer;
    }

    private bool IsThereOnlyOneTab()
    {
        return TabContainerList?.Count == 1;
    }

    private bool IsRemoveTabVisible(ContainerDto tabContainer)
    {
        return FormDesigner.IsSelectedContainer(tabContainer) && TabContainerList.Count > 1;
    }

    private async Task OnTabDeleteAsync(ContainerDto tabContainer)
    {
        int containerIndex = TabContainerList.IndexOf(tabContainer);
        TabContainerList.RemoveAt(containerIndex);

        containerIndex = containerIndex > TabContainerList.Count - 1 ? TabContainerList.Count - 1 : containerIndex;

        var previousTabContainer = TabContainerList[containerIndex];
        await OpenTabAsync(previousTabContainer);
    }

    #region Move Tab Left-Right Methods
    private bool IsMoveTabLeftVisible(ContainerDto tabContainer)
    {
        return TabContainerList.IsMoveLeftPossible(tabContainer);
    }

    private bool IsMoveTabRightVisible(ContainerDto tabContainer)
    {
        return TabContainerList.IsMoveRightPossible(tabContainer);
    }

    private async Task MoveTabLeftAsync(ContainerDto tabContainer)
    {
        TabContainerList.MoveLeft(tabContainer);
        await Task.CompletedTask;
    }

    private async Task MoveTabRightAsync(ContainerDto tabContainer)
    {
        TabContainerList.MoveRight(tabContainer);
        await Task.CompletedTask;
    }
    #endregion Move Row Up-Down Methods

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        var firstTabContainer = TabContainerList[0];
        ActiveTabContainer = firstTabContainer;
    }

}
